package com.game.spacetrader;


import com.game.spacetrader.TradeGoods.TradeGoodsView;
import com.game.spacetrader.map.MapView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Class is the activity for the game configuration page
 *
 */
public class GameConfig extends Activity implements OnClickListener, OnItemSelectedListener{
	private TextView remPoints, pilot, fighter, trader, engineer;
	private int points, pilotPoint, fighterPoint, traderPoint, engineerPoint;
	private Button addPilot, decPilot, addFighter, decFighter, addTrader, 
		decTrader, addEngineer, decEngineer, done;
	private Spinner difficulty;
	private String dif;
	private EditText name;
	
	/**
	 * Creates the page
	 */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.gameconfig);
		points = 16;
		dif="Beginner";
		pilotPoint=0; fighterPoint=0; traderPoint=0; engineerPoint=0;
		declareTextView();
		declareButtons();
		createSpinner();
	}
	
	/**
	 * Instantiates the TextViews and EditText
	 */
	public void declareTextView(){
		remPoints = (TextView) findViewById(R.id.pointsRemaining);
		pilot = (TextView) findViewById(R.id.pilot);
		fighter = (TextView) findViewById(R.id.fighter);
		trader = (TextView) findViewById(R.id.trader);
		engineer = (TextView) findViewById(R.id.engineer);
		name = (EditText) findViewById(R.id.playerName);
	}
	
	/**
	 * Instantiates and creates listeners for the buttons
	 */
	public void declareButtons(){
		addPilot = (Button) findViewById(R.id.addPilot);
		addPilot.setOnClickListener(this);
		decPilot = (Button) findViewById(R.id.decreasePilot);
		decPilot.setOnClickListener(this);
		
		addFighter = (Button) findViewById(R.id.addFighter);
		addFighter.setOnClickListener(this);
		decFighter = (Button) findViewById(R.id.decreaseFighter);
		decFighter.setOnClickListener(this);
		
		addTrader = (Button) findViewById(R.id.addTrader);
		addTrader.setOnClickListener(this);
		decTrader = (Button) findViewById(R.id.decreaseTrader);
		decTrader.setOnClickListener(this);
		
		addEngineer = (Button) findViewById(R.id.addEngineer);
		addEngineer.setOnClickListener(this);
		decEngineer = (Button) findViewById(R.id.decreaseEngineer);
		decEngineer.setOnClickListener(this);
		
		done = (Button) findViewById(R.id.done);
		done.setOnClickListener(this);
	}
	
	/**
	 * instantiates and creates Spinner option
	 */
	public void createSpinner(){
		difficulty = (Spinner) findViewById(R.id.difficultySpinner);
		String[] items = new String[] {"Beginner","Easy", "Normal", "Hard", "Impossible"}; 
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items); 
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
		difficulty.setAdapter(adapter); 
		difficulty.setOnItemSelectedListener(this);
	}

	/**
	 * Listener for when the user clicks on the screen
	 */
	public void onClick(View arg0) {
		switch(arg0.getId()){
		case R.id.addPilot:
			if(points>0){
				pilotPoint++;
				points--;
			}
			else
				Toast.makeText(getApplicationContext(), "You are out of points.",	Toast.LENGTH_LONG).show();
			break;
		case R.id.decreasePilot:
			pilotPoint--;
			points++;
			break;
		case R.id.addFighter:
			if(points>0){
				fighterPoint++;
				points--;
			}
			else
				Toast.makeText(getApplicationContext(), "You are out of points.",	Toast.LENGTH_LONG).show();
			break;
		case R.id.decreaseFighter:
			fighterPoint--;
			points++;
			break;
		case R.id.addTrader:
			if(points>0){
				traderPoint++;
				points--;
			}
			else
				Toast.makeText(getApplicationContext(), "You are out of points.",	Toast.LENGTH_LONG).show();
			break;
		case R.id.decreaseTrader:
			traderPoint--;
			points++;
			break;
		case R.id.addEngineer:
			if(points>0){
				engineerPoint++;
				points--;
			}
			else
				Toast.makeText(getApplicationContext(), "You are out of points.",	Toast.LENGTH_LONG).show();
			break;
		case R.id.decreaseEngineer:
			engineerPoint--;
			points++;
			break;
		case R.id.done:
			if(points>0)
				Toast.makeText(getApplicationContext(), "You still have points.",	Toast.LENGTH_LONG).show();
			else
				done();
		}
		updateTextView();
		
	}

	/**
	 * Updates all of the TextView with how many points are assigned
	 */
	public void updateTextView(){
		pilot.setText("Pilot: "+pilotPoint);
		fighter.setText("Fighter: "+fighterPoint);
		trader.setText("Trader: "+traderPoint);
		engineer.setText("Engineer: "+engineerPoint);
		remPoints.setText("Points Left: "+points);
	}
	
	/**
	 * Occurs when the user is done. Creates the Game
	 */
	public void done(){
		Game g = new Game(new Player(name.getText().toString(), pilotPoint, fighterPoint, traderPoint, engineerPoint), dif);
		Log.d("Name: ",""+name.getText().toString());
		Log.d("Pilot: ",""+pilotPoint);
		Log.d("Fighter: ",""+fighterPoint);
		Log.d("Trader: ",""+traderPoint);
		Log.d("Engineer: ",""+engineerPoint);
		Log.d("Difficulty: ",""+dif);
		g.start();
		Intent i = new Intent(this, MapView.class);
		startActivity(i);
	}
	
	/**
	 * Listener for when an object is selected in the Spinner
	 */
	public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
			long arg3) {
		String temp = parent.getItemAtPosition(pos).toString();
		if(temp.equals("Beginner") || temp.equals("Normal") || temp.equals("Easy") || temp.equals("Hard") || temp.equals("Impossible"))
			dif= parent.getItemAtPosition(pos).toString();
		
	}

	/**
	 * Creates option menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_space_trader, menu);
	    return true;
	}
	
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Handles if a key is pressed down
	 * @param keyCode 
	 * @param event
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			Intent i = new Intent(this, StartScreen.class);
			startActivity(i);
	    	return true;
	    }
	    else 
	    	return false ;
	}
	
	/**
     * Listener for the options menu
     * @param item the item in the Menu that is clicked
     */
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.travel:
	        	Intent i = new Intent(this, MapView.class);
	        	startActivity(i);
	            return true;
	        case R.id.market:
	        	Intent i2 = new Intent(this, TradeGoodsView.class); startActivity(i2);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}
